﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;

namespace devtm.AutoMapper
{
    public class OutputWriter
    {


        #region Ctors

        public OutputWriter()
            : this(VSConstants.GUID_OutWindowGeneralPane, "Output")
        {

        }


        public OutputWriter(Guid customOutput, string customTitle)
        {
            generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane;
            _title = customTitle;
        }

        #endregion


        IVsOutputWindow outWindow = Package.GetGlobalService(typeof(SVsOutputWindow)) as IVsOutputWindow;
        Guid generalPaneGuid;
        IVsOutputWindowPane generalPane;


        public void Write(string message, params object[] parameters)
        {

            IVsOutputWindowPane generalPane = GetPane();

            if (parameters != null && parameters.Length > 0)
                message = string.Format(message, parameters);

            generalPane.OutputString(message);
            generalPane.Activate();


        }

        public void WriteLine(string message, params object[] parameters)
        {

            IVsOutputWindowPane generalPane = GetPane();

            if (parameters != null && parameters.Length > 0)
                message = string.Format(message, parameters);

                
            generalPane.OutputString(message + "\n");


            generalPane.Activate();

        }

       
        private IVsOutputWindowPane GetPane()
        {

            if (generalPane != null)
                return generalPane;


            outWindow.GetPane(ref generalPaneGuid, out generalPane);

            if (generalPane == null)
                outWindow.CreatePane(ref generalPaneGuid, _title, 1, 1);

            outWindow.GetPane(ref generalPaneGuid, out generalPane);


            return generalPane;
        }


        public string _title { get; set; }

    }
}
